package com.example.administrator.dagger2demo.ui;

import android.os.Bundle;
import android.util.Log;

import com.example.administrator.dagger2demo.R;
import com.example.administrator.dagger2demo.di.AppComponent;
import com.example.administrator.dagger2demo.execption.ApiException;
import com.example.administrator.dagger2demo.http.RestApi;
import com.example.administrator.dagger2demo.model.ReleaseInfo;

import java.util.ArrayList;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends BaseActivity {


    @Inject
    Retrofit _retrofit;
    @Inject
    RestApi _rRestApi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void setUpActivityComponent(AppComponent appComponent) {
        appComponent.inject(this);
        Observable<ArrayList<ReleaseInfo>> observable =  _rRestApi.requestIndex();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ArrayList<ReleaseInfo>>() {
                    @Override
                    public void onCompleted() {
                        Log.i("TAG","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TAG","Throwable="+e.toString());
                        ApiException exception = ApiException.create(e, _retrofit);

                        Log.i("TAG","exception="+exception.getMessage());
                    }

                    @Override
                    public void onNext(ArrayList<ReleaseInfo> userInfos) {
                        Log.i("TAG","size="+userInfos.size());

                    }
                });

    }
}
